// pages/pinyin/pinyin.js.js
var app = getApp()
var pinyin = require('../../utils/pinyin.js');
Page({
  data: {
    pinyinText: '',

    access_token:'',
    inputValue:'',
    jieguo:'',
  },

  bindKeyInput: function (e) {
    console.log("用户搜索",e.detail.value)
    this.setData({
      inputValue: e.detail.value
    })
  },
  //搜索事件：点击事件
  search:function(e){
    var that = this
    console.log("点击搜索产生的数据",e.currentTarget.dataset)
    var searv = e.currentTarget.dataset.inputv
    //文本翻译api请求
    wx.request({
      url: 'https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1', //文本翻译的API
      data: {
        access_token:that.data.access_token,
        from:"en",
        to:"zh",
        q:searv,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log("翻译英文得到的数据",res.data)
        that.setData({
          jieguo:res.data.result.trans_result[0].dst,
        })
      }
    })
  },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this
  //获取access_token(鉴权认证机制)
      wx.request({
        url: 'https://aip.baidubce.com/oauth/2.0/token', //baidu的access_token 的 URL的地址
        data: {
          grant_type: 'client_credentials',
          client_id: 'l3AFd6hovOqsYFF7OvgT58qw',
          client_secret: 'GPhSkFfTH4Ljmptvkpl9ytrDQqiyOIdD'
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log("access_token的值",res.data.access_token)
          that.setData({
            access_token:res.data.access_token
          })
          console.log("access_token是否保存值",res.data.access_token)
     }
   })
    },


  // 事件处理函数
  onInput: function(e) {
    console.log(e)
    var char = e.detail.value;
    console.log('--', char);
    char = char && char.trim();
    if (char.length == 1) {
      if (pinyin.hasOwnProperty(char)) {
        console.log(pinyin[char].join(', '))
        this.setData({
          pinyinText: pinyin[char].join(', ')
        });
      }
      else {
        this.setData({
          pinyinText: '无结果嗷QAQ'
        });
      }
    }
    else {
      this.setData({
        pinyinText: ''
      });
    }
  },

})
